﻿using LxBasic;
using LxLESS;

namespace LxCore
{
    public abstract class RadiationSolver
    {
        public class Set : IterateSolver.Set<Unit>
        {
            public Set()
            {
                this.SolverType = IterationTypes.AMGV;
            }
            public override void CheckCreate()
            {
                base.CheckCreate();
                var list = new ConnectivitySolver().GetConnectivity(f => f.DomRadiation, c => c.Temperature);
                Solvers = new Unit[list.Count];
                for (int i1 = 0; i1 < Solvers.Length; i1++)
                {
                    var p = list[i1];
                    Solvers[i1] = new Unit()
                    {
                        Equations = p.Equations.ToArray(),
                        Connects = p.Connects.ToArray(),
                        InSolveSet = this,
                    };
                }
            }
        }
        public class Unit : IterateSolver.Unit
        {
            public DOMRadiation[] Equations;
            public EquationConnectBase[] Connects;
            public Set InSolveSet;
            public override void Solve_Steady()
            {
                if (Equations.Length == 0) return;
                //MultiThreads.RunSubThreads((threadInfo) =>
                //{
                //    foreach (var z in RadiationZones) { z.FormBoundValue(threadInfo); }
                //});
                for (int i1 = 0; i1 < Equations[0].DiscreteRadiations.Length; i1++)
                {
                    foreach (var z in Equations) { z.SetCurrentDir(i1); }
                    MultiThreads.RunParallelTask((threadInfo) =>
                    {
                        foreach (var z in Equations) { z.FormABOnFaces(threadInfo); }
                    });
                    MultiThreads.RunParallelTask((threadInfo) =>
                    {
                        foreach (var z in Equations) { z.FormABOnCells(threadInfo); }
                    });
                    RelaxSolveLimit(Equations, Connects);
                }
                MultiThreads.RunParallelTask((threadInfo) =>
                {
                    foreach (var z in Equations) { z.UpdateBoundsValue(threadInfo); }
                });
            }
            public override void Solve_ImplicitTrans()
            {
                Solve_Steady();
            }
            public override void Solve_ExplicitTrans()
            {
                throw new NotImplementedException();
            }
        }
    }

}

//public List<DOMRadiation> RadiationZones = new List<DOMRadiation>();
//public List<EquationConnectBase> RadiationConnects = new List<EquationConnectBase>();
//public override void Rearrange(ConnectivitySolver connectivityAnalysis)
//{
//    Solvers = new List<RadiationUnit>();
//    var list = connectivityAnalysis.GetConnectivity(RadiationZones, RadiationConnects);
//    foreach (var l in list)
//    {
//        RadiationUnit u = new RadiationUnit();
//        u.CreateUnit(l.Zones, l.Joints, SolverType, MaxPreSmoothSteps);
//        Solvers.Add(u);
//    }
//    RadiationZones.Clear();
//    RadiationConnects.Clear();
//}